package com.ruoyi.web.controller.market.sys;

import java.util.List;
import java.util.Map;

import com.ruoyi.web.wapper.SmsTemplateWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.SmsTemplate;
import com.ruoyi.sys.service.ISmsTemplateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 短信管理Controller
 * 
 * @author ruoyi
 * @date 2022-07-27
 */
@Controller
@RequestMapping("/sys/template")
public class SmsTemplateController extends BaseController
{
    private String prefix = "sys/template";

    @Autowired
    private ISmsTemplateService smsTemplateService;

    @RequiresPermissions("sys:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询短信管理列表
     */
    @RequiresPermissions("sys:template:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SmsTemplate smsTemplate)
    {
        startPage();
        List<Map<String, Object>> mapList = smsTemplateService.selectSmsTemplateListMap(smsTemplate);
        mapList = new SmsTemplateWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出短信管理列表
     */
    @RequiresPermissions("sys:template:export")
    @Log(title = "短信管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SmsTemplate smsTemplate)
    {
        List<SmsTemplate> list = smsTemplateService.selectSmsTemplateList(smsTemplate);
        ExcelUtil<SmsTemplate> util = new ExcelUtil<SmsTemplate>(SmsTemplate.class);
        return util.exportExcel(list, "短信管理数据");
    }

    /**
     * 新增短信管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存短信管理
     */
    @RequiresPermissions("sys:template:add")
    @Log(title = "短信管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SmsTemplate smsTemplate)
    {
        return toAjax(smsTemplateService.insertSmsTemplate(smsTemplate));
    }

    /**
     * 修改短信管理
     */
    @RequiresPermissions("sys:template:edit")
    @GetMapping("/edit/{otype}")
    public String edit(
            @PathVariable("otype") Long otype,
            @PathVariable("platform") String platform,
            ModelMap mmap
    )
    {
        SmsTemplate smsTemplate = smsTemplateService.selectSmsTemplateByOtypeAndPlatfrom(otype, platform);

        mmap.put("smsTemplate", smsTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存短信管理
     */
    @RequiresPermissions("sys:template:edit")
    @Log(title = "短信管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SmsTemplate smsTemplate)
    {
        return toAjax(smsTemplateService.updateSmsTemplate(smsTemplate));
    }

    /**
     * 删除短信管理
     */
    @RequiresPermissions("sys:template:remove")
    @Log(title = "短信管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(smsTemplateService.deleteSmsTemplateByOtypes(ids));
    }

    /**
     * 修改保存短信管理
     */
    @RequiresPermissions("sys:template:changeStatus")
    @Log(title = "短信管理", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("id") Long id)
    {
        return smsTemplateService.updateSmsTemplateStatus(id);
    }

}
